package top.melody.ecommerce.platform.handler.enums;

import top.melody.ecommerce.common.util.StringUtil;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: blentle
 * @group: rd
 * @createdate: 2016/10/31 15:16
 * @mail: blentle.huan.ren@gmail.com
 * @description:
 * @since:1.0
 */
public class SecurityRole implements Serializable {

    private static Map<String, SecurityRole> map = new HashMap<String, SecurityRole>();
    //超级管理员
    public static SecurityRole SUPER = new SecurityRole("super");
    //管理员
    public static SecurityRole ADMIN = new SecurityRole("admin");
    //普通用户
    public static SecurityRole OPERATOR = new SecurityRole("operator");

    private String code;

    private SecurityRole() {

    }

    private SecurityRole(String code) {
        if (!StringUtil.isEmpty(code)) {
            this.code = code.toLowerCase();
            map.put(this.code, this);
        }
    }

    public String getCode() {
        return this.code;
    }




    public static Collection<SecurityRole> getAll() {
        return map.values();
    }

    public static SecurityRole getByCode(String code) {
        if (!StringUtil.isEmpty(code)) {
            return map.get(code.toLowerCase());
        }
        return null;
    }

    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }

        if (obj instanceof SecurityRole) {
            return code.equalsIgnoreCase(((SecurityRole) obj).getCode());
        } else {
            return false;
        }
    }


    @Override
    public String toString() {
        return this.code.toString();
    }

    @Override
    public int hashCode() {
        return this.code.hashCode();
    }
}
